package com.cee.admin.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cee.admin.common.BaseController;
import com.cee.common.utils.StringUtils;
import com.cee.common.exception.GlobalException;
import com.cee.common.web.result.Result;
import com.cee.domain.modules.sys.entity.Role;
import com.cee.domain.modules.sys.vo.RoleVo;
import com.cee.ds.modules.sys.service.IRoleService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;

/**
 * <p>
 * ϵͳ 前端控制器
 * </p>
 *
 * @author zcj
 * @since 2020-12-07
 */
@Tag(name = "系统角色管理")
@RestController
@RequestMapping("/api/v1/sys/role")
@Slf4j
public class RoleController extends BaseController {
    @Autowired
    private IRoleService iRoleService;

    @Operation(summary = "分页查询角色信息")
    @GetMapping("/page")
    @RequiresPermissions(value = "sys:role:select")
    public Result<Page<RoleVo>> queryPage(Page<RoleVo> page, RoleVo roleVo) {
        try {
            return success(iRoleService.queryPageVo(page, roleVo));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }

    @Operation(summary = "获取角色")
    @GetMapping("getRole")
    @RequiresPermissions(value = "sys:role:select")
    public Result getRole(@Parameter(description = "角色主键") @RequestParam String id) {
        try {
            return success(iRoleService.getById(id));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }

    @Operation(summary = "保存或更新")
    @PostMapping("saveOrUpdate")
    @RequiresPermissions(value = "sys:role:write")
    public Result<Boolean> saveOrUpdate(@RequestBody Role role) {
        if (StringUtils.isBlank(role.getName())) {
            throw new GlobalException("角色名不能为空!");
        }
        try {
            //新增
            if (ObjectUtils.isEmpty(role.getId())) {
                role.setStatus(1);
                role.setIsDelete(false);
                LocalDateTime localDateTime = LocalDateTime.now();
                Date createTime = Date.from(localDateTime.atZone(ZoneId.systemDefault()).toInstant());
                role.setCreateTime(createTime);
            }
            return success(iRoleService.saveOrUpdate(role));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }

    }

    @PostMapping("/status")
    @Operation(summary = "更改角色状态")
    @RequiresPermissions(value = "sys:role:status:update")
    public Result<Boolean> changeStatus(@RequestBody Role role) {
        return success(iRoleService.changeStatus(role));
    }


    @Operation(summary = "批量删除")
    @PostMapping("deleteBatch")
    @RequiresPermissions(value = "sys:role:delete")
    public Result<Boolean> deleteBatch(@RequestBody List<Long> listId) {
        try {
            return success(iRoleService.deleteRoleListByIdList(listId));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }

    @Operation(summary = "删除")
    @DeleteMapping("delete/{roleId}")
    @RequiresPermissions(value = "sys:role:delete")
    public Result<Boolean> deleteBatch(@PathVariable Long roleId) {
        try {
            return success(iRoleService.deleteRoleListByIdList(Collections.singletonList(roleId)));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }

    /*
     * 获取角色下拉框数据
     * */
    @Operation(summary = "获取角色下拉框数据")
    @GetMapping("select-list")
//    @ApiLog(module = "角色管理", name = "获取角色下拉框数据")
    @RequiresPermissions(value = "sys:role:select")
    public Result<List<Role>> selectList() {
        return success(iRoleService.list());
    }

    /*
     * 获取角色下拉框数据
     * */
    @Operation(summary = "获取角色下拉框数据")
    @GetMapping("list")
//    @ApiLog(module = "角色管理", name = "获取角色下拉框数据")
    @RequiresPermissions(value = "sys:role:select")
    public Result<List<Map<String,Object>>> list() {
        try {
            return success(iRoleService.listMaps(new LambdaQueryWrapper<Role>()
                    .eq(Role::getIsDelete,false)));
        } catch (Exception e) {
            log.error(e.getMessage());
            return error("系统错误，请联系管理员！");
        }
    }
}

